﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using YJYpublicClass;

namespace YJYpublicClass
{
   public class PLC_Modulbus :PLC通讯接口
    {

       HslCommunication.ModBus.ModbusTcpNet Modbus = new HslCommunication.ModBus.ModbusTcpNet();
        public bool Connection
        {
            get;
            set;
        }

        public string Mes
        {
            get;
            set;
        }

        private PLCconfig cft = new PLCconfig();
        public ConfigBase cf
        {
            get { return cft; }
            set { cft = (PLCconfig)value; }
        }

        public void Connect()
        {
            Modbus.IpAddress = cft.IP;
            Modbus.Port = cft.port;
            Modbus.Station = cft.station;
            Modbus.DataFormat =(HslCommunication.Core.DataFormat) cft.readtype;
            if (Modbus.ConnectServer().IsSuccess)
            {
                Connection = true;
            }
            else
            {
                Connection = false;
            }
        }

        public void Close()
        {
            Modbus.ConnectClose();
        }

        public void ReadD(string address, out string value)
        {
                string result = "-1";
                try
                {

                    if (Connection)
                    {
                        HslCommunication.OperateResult<UInt16> t = Modbus.ReadUInt16(address);
                        if (t.IsSuccess)
                        {
                            result = t.Content.ToString();
                            value = result;
                        }
                        else
                        {
                            value = result;
                        }

                    }
                    else
                    {
                        value = result;
                    }
                }
                catch (Exception ex)
                {
                    value = result;
                    Mes = ex.ToString();
                }

        }

        public Boolean WriteD(string address, string value)
        {
            try
            {
                ushort valuet = Convert.ToUInt16(value);
                if (Connection)
                {
                    if (Modbus.Write(address, valuet).IsSuccess)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public void ReadF(string address, out string value)
        {
            string result = "-1";
            try
            {

                if (Connection)
                {
                    HslCommunication.OperateResult<float> t = Modbus.ReadFloat(address);
                    if (t.IsSuccess)
                    {
                        result = t.Content.ToString();
                        value = result;
                    }
                    else
                    {
                        value = result;
                    }

                }
                else
                {
                    value = result;
                }
            }
            catch (Exception ex)
            {
                value = result;
                Mes = ex.ToString();
            }
        }

        public Boolean WriteF(string address, string value)
        {
            try
            {
                float valuet = 0.0f;
                float.TryParse(value,out valuet);
                if (Connection)
                {
                    if (Modbus.Write(address, valuet).IsSuccess)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public void ReadDouble(string address, out string value)
        {
            string result = "-1";
            try
            {

                if (Connection)
                {
                    HslCommunication.OperateResult<double> t = Modbus.ReadDouble(address);
                    if (t.IsSuccess)
                    {
                        result = t.Content.ToString();
                        value = result;
                    }
                    else
                    {
                        value = result;
                    }

                }
                else
                {
                    value = result;
                }
            }
            catch (Exception ex)
            {
                value = result;
                Mes = ex.ToString();
            }
        }

        public Boolean WriteDouble(string address, string value)
        {
            try
            {
                double valuet = 0.0;
                double.TryParse(value, out valuet);
                if (Connection)
                {
                    if (Modbus.Write(address, valuet).IsSuccess)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public void ReadCoil(string address, out string value)
        {
            string result = "-1";
            try
            {

                if (Connection)
                {
                    HslCommunication.OperateResult<Boolean> t = Modbus.ReadCoil(address);
                    if (t.IsSuccess)
                    {
                        result = t.Content.ToString();
                        value = result;
                    }
                    else
                    {
                        value = result;
                    }

                }
                else
                {
                    value = result;
                }
            }
            catch (Exception ex)
            {
                value = result;
                Mes = ex.ToString();
            }
        }

        public Boolean WriteCoil(string address, bool value)
        {
            try
            {
               
                if (Connection)
                {
                    if (Modbus.Write(address, value).IsSuccess)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
    public class PLCconfig :ConfigBase
    {
        [Category("Modulbus参数"), Description("int类型"), DisplayName("端口号"), ReadOnly(false)]
        public int port { get; set; }

        [Category("Modulbus参数"), Description("string类型"), DisplayName("IP地址"), ReadOnly(false)]
        public string IP { get; set; }
        [Category("Modulbus参数"), Description("byte类型"), DisplayName("站号"), ReadOnly(false)]
        public byte station { get; set; }

        [Category("Modulbus参数"), Description("int类型"), DisplayName("读取顺序"), ReadOnly(false)]
        public int readtype { get; set; }
        //AOI前工位相关地址
        [Category("通讯协议参数"), Description("string类型"), DisplayName("01前工位启动拍照地址"), ReadOnly(false)]
        public string QStartaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("02前工位结果地址"), ReadOnly(false)]
        public string QResultaddress { get; set; }

        [Category("通讯协议参数"), Description("string类型"), DisplayName("03中工位启动拍照地址"), ReadOnly(false)]
        public string ZStartaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("04中工位结果地址"), ReadOnly(false)]
        public string ZResultaddress { get; set; }

        [Category("通讯协议参数"), Description("string类型"), DisplayName("05后工位启动拍照地址"), ReadOnly(false)]
        public string HStartaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("06后工位结果地址"), ReadOnly(false)]
        public string HResultaddress { get; set; }


        //扫码识别工件相关地
        [Category("通讯协议参数"), Description("string类型"), DisplayName("07扫码器1启动拍照地址"), ReadOnly(false)]
        public string S1ScanStartaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("08扫码器1读码结果地址"), ReadOnly(false)]
        public string S1ScanResultaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("09扫码器1绑定结果地址"), ReadOnly(false)]
        public string S1DealResultaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("10扫码器1机台号地址"), ReadOnly(false)]
        public string S1DeviNumaddress { get; set; }

        [Category("通讯协议参数"), Description("string类型"), DisplayName("11扫码器2启动拍照地址"), ReadOnly(false)]
        public string S2ScanStartaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("12扫码器2读码结果地址"), ReadOnly(false)]
        public string S2ScanResultaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("13扫码器2绑定结果地址"), ReadOnly(false)]
        public string S2DealResultaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("14扫码器2机台号地址"), ReadOnly(false)]
        public string S2DeviNumaddress { get; set; }







        //通讯协议参数
        [Category("通讯协议参数"), Description("string类型"), DisplayName("启动拍照地址"), ReadOnly(false)]
        public string  Startaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("X偏移坐标地址"), ReadOnly(false)]
        public string Xaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("Y偏移坐标地址"), ReadOnly(false)]
        public string Yaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("R偏移坐标地址"), ReadOnly(false)]
        public string Raddress { get; set; }



        [Category("通讯协议参数"), Description("string类型"), DisplayName("请求移动地址"), ReadOnly(false)]
        public string RMaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("请求移动X地址"), ReadOnly(false)]
        public string RMXaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("请求移动Y地址"), ReadOnly(false)]
        public string RMYaddress { get; set; }
        [Category("通讯协议参数"), Description("string类型"), DisplayName("请求移动R地址"), ReadOnly(false)]
        public string RMRaddress { get; set; }

        public PLCconfig()
        {
            IP = "192.168.1.88";
            port = 502;
            station = 1;
            readtype = 3;

        }
    }
}
